package me.learningandroid.ch2_7.uicomponentdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class Splash2Activity extends AppCompatActivity {
    private static final String BUNDLE_PROGRESS="progress";
    private static final String BUNDLE_GENDERNOTSELECTED="genderNotSelected";
    private static final String BUNDLE_IMAGEUPLOADED="imageUploaded";
    private static final int MAX_PROGRESS=5;

    /** 进度 */
    private int progress =0;
    /** 性别已选择 */
    private boolean genderNotSelected = false;
    /** 图片已上传 */
    private boolean imageUploaded = false;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, Splash2Activity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // `setContentView`让
        setContentView(R.layout.activity_splash2);

        EditText editTextName = findViewById(R.id.editTextName);
        EditText editTextPassword = findViewById(R.id.editTextPassword);
        RadioGroup radioGroupGender = findViewById(R.id.radioGender);
        ImageButton uploadBtn = findViewById(R.id.uploadBtn);
        ImageView imageViewAvatar = findViewById(R.id.avatar);
        CheckBox agreeCheckbox = findViewById(R.id.agreeCheckbox);
        ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setMax(MAX_PROGRESS);
        Button submitBtn = findViewById(R.id.submitBtn);

        FloatingActionButton nextBtn = findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener((view)-> {
            Splash3Activity.startActivity(this);
        });

        editTextName.addTextChangedListener(new TextWatcher() {
            int beforeLen=0;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeLen=s.length();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (beforeLen>0 && s.length()==0) {
                    progress -= 1;
                } else if (beforeLen==0 && s.length()>0) {
                    progress += 1;
                }
                progressBar.setProgress(progress);
            }
        });

        editTextPassword.addTextChangedListener(new TextWatcher() {
            int beforeLen=0;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeLen=s.length();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (beforeLen>0 && s.length()==0) {
                    progress -= 1;
                } else if (beforeLen==0 && s.length()>0) {
                    progress += 1;
                }
                progressBar.setProgress(progress);
            }
        });
        radioGroupGender.setOnCheckedChangeListener((radioGroup, checkedId)->{
            if (!genderNotSelected) {
                genderNotSelected=true;
                progress += 1;
                progressBar.setProgress(progress);
            }
        });
        uploadBtn.setOnClickListener((view)->{
            imageViewAvatar.setVisibility(View.VISIBLE);
            if (!imageUploaded) {
                imageUploaded=true;
                progress += 1;
                progressBar.setProgress(progress);
            }
        });
        agreeCheckbox.setOnCheckedChangeListener((btn, isChecked) -> {
            if (isChecked) {
                progress += 1;
            } else {
                progress -= 1;
            }
            progressBar.setProgress(progress);
        });

        submitBtn.setOnClickListener((v) -> {
            if (progress!=MAX_PROGRESS) {
                Toast.makeText(this,"请完善表单",Toast.LENGTH_LONG).show();
                return;
            }
            Toast.makeText(this,"表单已提交",Toast.LENGTH_LONG).show();
        });
    }


    // 安卓可能会随时重启活动，会主动调用这个方法保存临时状态。
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(BUNDLE_PROGRESS, progress);
        outState.putBoolean(BUNDLE_GENDERNOTSELECTED, genderNotSelected);
        outState.putBoolean(BUNDLE_IMAGEUPLOADED, imageUploaded);
    }

    // 从bundle中恢复临时变量的值
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        progress = savedInstanceState.getInt(BUNDLE_PROGRESS);
        genderNotSelected = savedInstanceState.getBoolean(BUNDLE_GENDERNOTSELECTED);
        imageUploaded = savedInstanceState.getBoolean(BUNDLE_IMAGEUPLOADED);

        ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setProgress(progress);
        ImageView imageViewAvatar = findViewById(R.id.avatar);
        if (imageUploaded) {
            imageViewAvatar.setVisibility(View.VISIBLE);
        }
    }

}